--http://logging.admicro.vn/
-- _adv.gif?
-- adm_domain=http%3A//dantri.com.vn/&adm_campaign=1029208&adm_aditem=172928&adm_zoneid=221&adm_channelid=-1&adm_random=0.1505014793947339

local biz_name = "_adv.gif"
if test then
    ConfigRedisConnectors[biz_name]={
        host = test.host,
        port = test.port,
        queue = "queue_1"
    }
else
    ConfigRedisConnectors[biz_name]={
        --host = "192.168.2.28",
        --port = 6379,
        --203
        --host = "192.168.6.203",
        --host = "192.168.5.141",
        host = "192.168.5.201",
		port = 2468,
        queue = "queue_1"
        
        
    }
end

local function _adv_gif()
return coroutine.create(function ()
-- constants
local REDIS_SEPERATOR = config.SEPARATE_SYN
local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
local http_cookie_domain = config.http.setcookie.pc
local http_server_name = config.http.servername
local cov = "0"
-- headers
local local_config = config
local COOKIE_HEADER = local_config.COOKIE_HEADER
local USER_AGENT_HEADER = local_config.USER_AGENT_HEADER
local SET_COOKIE_HEADER = local_config.SET_COOKIE_HEADER
local REFERER_HEADER = local_config.REFERER_HEADER
local SERVER_HEADER = local_config.SERVER_HEADERparaTable
local PRAGMA_HEADER = local_config.PRAGMA_HEADER
local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
local EXPIRES_HEADER = local_config.EXPIRES_HEADER
local LAST_MODIFIED_HEADER = local_config.LAST_MODIFIED_HEADER
local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
--cookies
local REGION_COOKIE = local_config.REGION_COOKIE
local GUID_COOKIE = local_config.GUID_COOKIE
local CREATE_COOKIE = local_config.CREATE_COOKIE
local EXPIRE_TIME = local_config.EXPIRE_TIME
-- functions
local reply_pushHeader = reply.pushHeader
local reply_ok = reply.status_type.ok
local reply_setStatus = reply.setStatus
local request_getHeadersKV = request.getHeadersKV
local request_getParamsKV = request.getParamsKV
local request_getIP = request.getIP
local os_time = os.time
local to_number = tonumber
local util_redis_cmd = util.redis_cmd
local util_parseKV = util.parseKV
local luredis_clear_buffer = luredis.clear_buffer
local luredis_concat_buffer = luredis.concat_buffer
local luredis_send_buffer = luredis.send_buffer
local url_decode = util.fast_url_decode
local l_pushCookieHeader = pushCookieHeader
local util_create_guid = util.create_guid
local coroutine_yield = coroutine.yield
--_adv_gif parameters --
local ADM_DOMAIN = local_config.ADM_DOMAIN_PARAM
local ADM_ADITEM = local_config.ADM_ADITEM_PARAM
local ADM_ADITEM_DEF = local_config.ADM_ADITEM_STRING_DEFAULT
local ADM_ZONEID = local_config.ADM_ZONEID_PARAM
local ADM_ZONEID_DEF = local_config.ADM_ZONEID_STRING_DEFAULT
local ADM_CAMPAIGN = local_config.ADM_CAMPAIGN_PARAM
local ADM_CAMPAIGN_DEF = local_config.ADM_CAMPAIGN_STRING_DEFAULT
local ADM_CHANNELID = local_config.ADM_CHANNELID_PARAM
local ADM_CHANNELID_DEF = local_config.ADM_CHANNELID_STRING_DEFAULT
local log_business = util.log_business_count
while true do
    -- reply
    reply_pushHeader(CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
    reply_pushHeader(PRAGMA_HEADER, "no-cache")
    reply_pushHeader(EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:1paraTable0 GMT")
    reply_pushHeader(CONTENT_TYPE_HEADER, "image/gif")
    reply_pushHeader(LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
    reply_pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
    reply_pushHeader(SERVER_HEADER, http_server_name)
    -- headers
    local headerTable = request_getHeadersKV();
    -- cookie
    local cookieString = headerTable[COOKIE_HEADER];
    local cookieTable = util_parseKV(cookieString,";=");

    local create_cookie = cookieTable[CREATE_COOKIE]
    local guid = cookieTable[GUID_COOKIE];

    local newguid = "0";
    local create_time = os_time()
    local expire_time = EXPIRE_TIME+create_time;
    local ip = request_getIP()

    -- guid
    if not guid then
        guid = util_create_guid(create_time, ip);
        if guid then
            create_cookie = create_time
            local expire_time = EXPIRE_TIME+create_time;
            l_pushCookieHeader(GUID_COOKIE, guid, expire_time, "/", http_cookie_domain, false)
            l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", http_cookie_domain, false)
        else
            guid = "0"
        end
        newguid="1"
    end
    
	if not create_cookie then 
    	create_cookie = create_time
	    l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", http_cookie_domain, false)
    end
    
    -- get region
    local region = cookieTable[REGION_COOKIE];
    if not region then -- ask redis for region
		region = "-1"
    end

    local paraTable = request_getParamsKV("&=");
    local aditem = paraTable[ADM_ADITEM];
    if not to_number(aditem) then aditem = ADM_ADITEM_DEF end

    local adm_domain = url_decode(paraTable[ADM_DOMAIN])
    local zoneID = paraTable[ADM_ZONEID];
    if not to_number(zoneID)  then zoneID = ADM_ZONEID_DEF end

    local campaign = paraTable[ADM_CAMPAIGN];
    if not to_number(campaign) then campaign = ADM_CAMPAIGN_DEF end

    local chanelID = paraTable[ADM_CHANNELID]
    if not to_number(chanelID) then chanelID = ADM_CHANNELID_DEF end
    -----------------------
    local referer =  headerTable[REFERER_HEADER]
    if not referer then
    	referer = "-1"
    end
    
    -- get region
    local region = cookieTable[REGION_COOKIE];
    if not region then -- ask redis for region
		region = "-1"
    end
    
    luredis_clear_buffer()
    luredis_concat_buffer(REDIS_SEPERATOR,
        create_time, ip,  headerTable[USER_AGENT_HEADER], create_cookie,
        aditem,  "1",  guid, adm_domain,  "0",  cov,
        zoneID,  campaign,  chanelID,  newguid, referer, region)
    if util_redis_cmd(biz_name,true,luredis_send_buffer,lpush_cmd_format) then
    	reply_setStatus(reply_ok)
    	log_business("adv")
    end
    coroutine_yield()
end
end)
end

creators[biz_name] = function()
	f[biz_name] = _adv_gif()
end
